package work.mediway.mdm.controller.biz;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.mediway.hos.base.model.BaseResponse;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import work.mediway.mdm.biz.dto.biz.DictEditLogDTO;
import work.mediway.mdm.biz.dto.biz.DictLogDTO;
import work.mediway.mdm.biz.dto.biz.PatientEditLogDTO;
import work.mediway.mdm.biz.service.biz.DictLogService;
import work.mediway.mdm.biz.service.biz.MdmEditLogService;
import work.mediway.mdm.biz.vo.biz.DictEditLogVO;
import work.mediway.mdm.biz.vo.biz.PatientEditLogVO;
import work.mediway.mdm.core.annotation.OpsLog;
import work.mediway.mdm.core.enums.CodeAndMessageEnum;
import work.mediway.mdm.core.enums.OpsLogTypeEnum;
import work.mediway.mdm.core.helper.BaseDatabaseHelper;
import work.mediway.mdm.core.page.QueryRequest;
import work.mediway.mdm.core.vo.TableVO;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2020/11/27
 */
@Api(tags = "日志模块")
@ApiSupport(order = 11)
@Validated
@RequestMapping("log")
@RestController
@RequiredArgsConstructor
public class DictLogController {

    private final MdmEditLogService mdmEditLogService;
    private final DictLogService dictLogService;
    private final BaseDatabaseHelper baseDatabaseHelper;

    @OpsLog(value = "字典变动日志", type = OpsLogTypeEnum.SELECT)
    @PostMapping("change/dict")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "字典变动日志")
    public BaseResponse<Object> dictChangeLog(DictLogDTO dictLogDTO, QueryRequest queryRequest) {
        List<TableVO> tableLog = this.baseDatabaseHelper.getTable(dictLogDTO.getTableCode());
        if (CollUtil.isNotEmpty(tableLog) && tableLog.size() != 0) {
            Page<Map<String, Object>> page = this.dictLogService.page(dictLogDTO,queryRequest);
            return CollUtil.isNotEmpty(page.getRecords()) ? BaseResponse.success(page) : BaseResponse.error(String.valueOf(CodeAndMessageEnum.DB_RESOURCE_NULL.getCode()), CodeAndMessageEnum.DB_RESOURCE_NULL.getMessage());
        } else {
            List<Map<String, Object>> list = CollUtil.newArrayList();
            DictEditLogDTO dictEditLogDTO = new DictEditLogDTO();
            dictEditLogDTO.setTableCode(dictLogDTO.getTableCode());
            dictEditLogDTO.setTableRowId(dictLogDTO.getMapUserId());
            Page<DictEditLogVO> page = this.mdmEditLogService.listDictChangeLog(dictEditLogDTO, queryRequest);

            for (DictEditLogVO record : page.getRecords()) {
                Map<String, Object> data = MapUtil.newHashMap();
                for (DictEditLogVO.ChangeDetail detail : record.getDetails()) {
                    data.put(detail.getFieldName(), detail.getNewValue());
                }
                data.put("operatetype", record.getEditTypeCode());
                data.put("mapuserid", record.getTableRowId());
                list.add(data);
            }
            return CollUtil.isNotEmpty(page.getRecords()) ? BaseResponse.success(page) : BaseResponse.error(String.valueOf(CodeAndMessageEnum.DB_RESOURCE_NULL.getCode()), CodeAndMessageEnum.DB_RESOURCE_NULL.getMessage());
        }
    }

    @OpsLog(value = "患者变动日志", type = OpsLogTypeEnum.SELECT)
    @GetMapping("change/patient")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "患者变动日志")
    public BaseResponse<Page<PatientEditLogVO>> patientChangeLog(@Valid PatientEditLogDTO editLogDTO, QueryRequest queryRequest) {
        Page<PatientEditLogVO> page = this.mdmEditLogService.listPatientChangeLog(editLogDTO, queryRequest);
        return CollUtil.isNotEmpty(page.getRecords()) ? BaseResponse.success(page) : BaseResponse.error(String.valueOf(CodeAndMessageEnum.DB_RESOURCE_NULL.getCode()), CodeAndMessageEnum.DB_RESOURCE_NULL.getMessage());
    }


}